package com.yueyang.sojourn.server.service.sojourn.merch.inventory;

import com.yueyang.sojourn.server.model.po.SojournMerchInventory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yemingxing
 * @summary
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-04-05 16:42
 */
@Service
@Slf4j
public class SojournMerchInventoryService {

    @Resource
    private SojournMerchInventoryMapper sojournMerchInventoryMapper;


    public int insert(SojournMerchInventory record) {
        return sojournMerchInventoryMapper.insert(record);
    }


    public SojournMerchInventory selectByPrimaryKey(Long id) {
        return sojournMerchInventoryMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(SojournMerchInventory record) {
        return sojournMerchInventoryMapper.updateByPrimaryKeySelective(record);
    }


    public int batchInsert(List<SojournMerchInventory> list) {
        return sojournMerchInventoryMapper.batchInsert(list);
    }

    public List<SojournMerchInventory> selectByMerchIdAndEffectDate(long merchId, Date effectDateStart, Date effectDateEnd) {
        Assert.notNull(effectDateStart, "effectDateStart 不能为空");
        Assert.notNull(effectDateEnd, "effectDateEnd 不能为空");
        return sojournMerchInventoryMapper.selectByMerchIdAndEffectDate(merchId, effectDateStart, effectDateEnd);
    }

    /**
     * 减库存，乐观锁更新
     *
     * @param id      扣减库存的id
     * @param number  减去库存数量
     * @param version 版本号
     * @return 减库存是否成功
     */
    public boolean takeInventory(long id, int number, Integer version) {
        return sojournMerchInventoryMapper.takeInventory(id, number, version) > 0;
    }

    /**
     * 加库存，乐观锁更新
     *
     * @param id      扣减库存的id
     * @param number  减去库存数量
     * @param version 版本号
     * @return 加库存是否成功
     */
    public boolean addInventory(long id, int number, Integer version) {
        return sojournMerchInventoryMapper.addInventory(id, number, version) > 0;
    }

    public List<SojournMerchInventory> selectByMerchId(Long merchId) {
        return sojournMerchInventoryMapper.selectByMerchId(merchId);
    }
}
